﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities.Resumes;
using Forest.Core;
using Forest.Core.Data;
using Forest.Core.Caching;
using Forest.Core.Events;
using Forest.Cat.Domain.Shared;

namespace Forest.Cat.Domain.Services.Resumes
{
    public class TrainingService : ITrainingService
    {
        #region Constants
        private const string Training_BY_ID_KEY = "training.id-{0}";
        private const string Training_PATTERN_KEY = "training.";
        #endregion

        #region Fields
        private readonly IRepository<Training> _trainingRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        private readonly IGenerateNumberService _generateNumberService;
        #endregion

        #region Ctor
        public TrainingService(IRepository<Training> workExperienceRepository,
            ICacheManager cacheManager, IEventPublisher eventPublisher, IGenerateNumberService generateNumberService)
        {
            _trainingRepository = workExperienceRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
            _generateNumberService = generateNumberService;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteTraining(Training training)
        {
            if (training == null)
                throw new ArgumentNullException("Training");

            _trainingRepository.Delete(training);

            _cacheManager.RemoveByPattern(Training_PATTERN_KEY);
            //event notification
            _eventPublisher.EntityDeleted(training);

        }

        public Training GetTrainingById(Guid trainingId)
        {
            if (trainingId == Guid.Empty)
                return null;

            string key = string.Format(Training_BY_ID_KEY, trainingId);

            return _cacheManager.Get(key, () =>
            {
                var cl = _trainingRepository.GetById(trainingId);
                return cl;
            });
        }

        public virtual IPagedList<Training> GetAllTrainings(int pageIndex, int pageSize)
        {
            var query = _trainingRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<Training>(query, pageIndex, pageSize);
            return items;
        }

        public virtual IList<Training> GetTrainings(Guid resumeId)
        {
            var query = _trainingRepository.Table;
            query = query.Where(c => c.ResumeId == resumeId).OrderByDescending(c => c.LastUpdated);

            return query.ToList();
        }

        public virtual void InsertTraining(Training training)
        {
            if (training == null)
                throw new ArgumentNullException("Training");

            _trainingRepository.Insert(training);

            _cacheManager.RemoveByPattern(Training_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(training);
        }

        public virtual void UpdateTraining(Training training)
        {
            if (training == null)
                throw new ArgumentNullException("Training");
            _trainingRepository.Update(training);

            _cacheManager.RemoveByPattern(Training_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(training);
        }
        #endregion

        public Guid CreateId
        {
            get
            {
                return _generateNumberService.GenerateId<Training>(_trainingRepository, GenerateType.Training);
            }
        }

        public bool HasTrainings(Guid resumeId)
        {
            return _trainingRepository.Table.Any(s => s.ResumeId == resumeId);
        }
    }
}
